package com.lby.springcloud.ddd.usercontext.south.adapter.repository;

import com.lby.springcloud.ddd.usercontext.domain.User;
import com.lby.springcloud.ddd.usercontext.south.port.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component("userJDBCRepository")
public class UserJDBCRepository implements UserRepository {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public User findbyId(long userId) {
		User user = null;
		String sql = "select user_id,nickname,mobile_no,password  from t_user where user_id = ?";
		UserPO userPO = jdbcTemplate.queryForObject(sql, new Object[]{userId}, new BeanPropertyRowMapper<>(UserPO.class));
		if(userPO != null) {
			user = UserAdapter.change(userPO);
		}
		return user;
	}

	@Override
	public User findbyAccount(String account) {
		User user = null;
		String sql = "select user_id,nickname,mobile_no,password  from t_user where mobile_no = ?";
		UserPO userPO = jdbcTemplate.queryForObject(sql, new Object[]{account}, new BeanPropertyRowMapper<>(UserPO.class));
		if(userPO != null) {
			user = UserAdapter.change(userPO);
		}
		return user;
	}
	
}
